package com.starbaba.activity.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.util.StringUtils;
import com.jumai.server.qiniu.QiniuUploadUtils;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.starbaba.activity.model.LoanProductVO;
import com.starbaba.activity.service.LoanActivityProductService;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;

/**
 * 贷款产品管理
 * @version 1.0
 */
@Component
public class LoanActivityProductServiceImpl implements LoanActivityProductService {
    
	@SuppressWarnings("deprecation")
    private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
    @Override
    @SuppressWarnings("rawtypes")
	public List<LoanProductVO> getDataList(int iDisplayStart, int iDisplayLength, PageResponse pageResponse, Map<String, Object> paramsMap) {
        
        String activityid = (String) paramsMap.get("activityid");
		String status = (String) paramsMap.get("status");
		String name = (String) paramsMap.get("name");

		String sql = "select c.id,c.name,c.summary,c.tag,c.url,"
		        + "c.logo,c.status,c.ratetype,c.rate,c.ctime,c.operator,c.type from loan_product_info c where 1=1 ";
		
		String selectSql = "";
		if (!StringUtils.isEmpty(activityid)) {
            selectSql += " and c.id = " + activityid ;
        }
		if (!StringUtils.isEmpty(name)) {
			selectSql += " and c.name like '%" +name + "%'";
		}
		if (!StringUtils.isEmpty(status)) {
            selectSql += " and c.status = '" + status + "'";
        }

		sql += selectSql;
		sql += " order by c.status asc limit " + iDisplayStart + " , " + iDisplayLength;

		String countSql = "select count(1) from loan_product_info c where 1=1 " + selectSql;
		List<LoanProductVO> list = null;
		try {
			int count = dao.getInt(countSql);
			list = dao.list(sql, LoanProductVO.class, new Object[] {});
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		} catch (AppException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public void save(Map<String, Object> paramsMap, LoanProductVO bean) {

//		String countsql = "select position+1 from loan_product_info ";
//		LoanProductVO model = setValue(bean);
//		int position = 0;
//		try {
//			position = dao.getInt(countsql, new Object[] {});
//			model.setPosition(position);
//			dao.add(model);
//		} catch (AppException e) {
//			e.printStackTrace();
//		}
	}

	public LoanProductVO setValue(LoanProductVO bean) {

		LoanProductVO model = null;
		try {
			model = new LoanProductVO();
			String thumbnail = null;
			File thumbnailFile = bean.getImg();
			if (thumbnailFile != null) {
				thumbnail = uploadToQiniu(thumbnailFile);
				model.setLogo(thumbnail);
			}
			model.setSummary(bean.getSummary());
			model.setName(bean.getName());
			model.setUrl(bean.getUrl());
			model.setFlow_text(bean.getFlow_text());
			model.setFlow_pic(bean.getFlow_pic());
			model.setRate(bean.getRate());
			model.setRatetype(bean.getRatetype());
			model.setLenddate(bean.getLenddate());
			model.setLoanlimit(bean.getLoanlimit());
			model.setType(bean.getType()); // 活动类型
			if (bean.getStatus() != null) {
				model.setStatus(bean.getStatus());
			}
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = format.format(new Date());
            model.setUtime(date);
            model.setOperator(bean.getOperator());
			model.setTag(bean.getTag());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	public String uploadToQiniu(File srcFile) {
		String iconUrl = null;
		if (srcFile.isDirectory()) {
			return iconUrl;
		} else {
			try {
				FileInputStream in = new FileInputStream(srcFile);
				iconUrl = QiniuUploadUtils.updateImg("loan", in, null, new Configuration(Zone.zone0()));
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return iconUrl;
	}

	@Override
	public void update(Map<String, Object> paramsMap, LoanProductVO bean) {
		
//		boolean isTrue = true;
//		LoanProductVO model = setValue(bean);
//		model.setId(bean.getId());
//		try {
//			int result = dao.update(model, new String[] { "id" });
//			StringBuffer strSql = new StringBuffer();
//			if (result > 0 && !isTrue){  // 更新上/下线时间为空
//				dao.update("update loan_product_info a set "+ strSql.toString() +" where a.id = ?", bean.getId());
//			}
//		} catch (Exception e) {
//			LogService.error(e);
//		}
	}
}
